import express from "express";
import expressSession from "express-session";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import uuid from "uuid";
import url from "url"

//创建服务
const server = express();

//静态资源
server.use(express.static("./../src/"));

//配置中间件
server.use(expressSession({
    name: "useker.cn",
    genid: function () {
        return uuid.v1;
    },
    maxAge: 1000 * 60 * 20,
    secret: "useker.cn",
    saveUninitialized: true,
    resave: true
}))

server.use(bodyParser.urlencoded({
    extended: false
}));
server.use(bodyParser.json());
server.use(cookieParser("useker.cn"));


//预设 白名单
let WhiteList = ["/api/login", "/api/reg", "/api/checkuser", "/api/products", "/api/getproduct"]; // 登录,注册 不拦截
//拦截器
server.all("*", (request, response, next) => {
    response.setHeader("Content-Type", "application/json;charset=utf-8");
    response.setHeader("Access-Control-Allow-Origin", "*");

    // if (WhiteList.indexOf(url.parse(request.originalUrl, true).pathname.toLowerCase()) != -1) {
    //     next();
    // } else {
    //     if (request.session["isLogin"] != undefined || request.session["isLogin"] != null) {
    //         next();
    //     } else {
    //         response.json({
    //             msg: "您还没登录,请先登录!",
    //             status: -500
    //         })
    //     }
    // }
    next();

})

//配置保安大爷
server.use("/api", require("./routes/ApiRoute").apiRouter);

server.listen(8080, () => {
    console.log("服务启动完毕");
})
